/**
 * Created by Administrator on 2017/3/21.
 */
import React, {Component} from 'react';
import {
  Form, Input, Radio, Select, Typography,
  Upload, Tooltip, DatePicker, Button,
} from 'antd';
import {connect} from 'dva';
import ReactDataGrid from 'react-data-grid';
import moment from 'moment'
import find from 'lodash/find'
import request from '@/utils/request';
const { Text } = Typography;
const FormItem = Form.Item;
const { TextArea } = Input;
const RadioGroup = Radio.Group;
const Option = Select.Option;
class AddPoliciesForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      users:[],
      fileList:[],
      meterArr: [],
      address:[{ name: "单位"},
        { name: "旬报"},
        { name: "月累计"},
        { name: "季累计"},
        { name: "年累计"},
        { name: "完成年计划%"}]
    };
  }

  componentDidMount() {
    const that=this;
    let arr=[
      {name:'供水量',key:'gsl',unit:'万立方'},
      {name:'销水量',key:'xsl',unit:'万立方'},
      {name:'销售额',key:'xse',unit:'万 元'},
      {name:'排水收入',key:'pssr',unit:'万 元'},
      {name:'排水量',key:'psl',unit:'万立方'},
      {name:'校表收入',key:'xbsr',unit:'千 元'},
      {name:'维修',key:'wx',unit:'户'},
      {name:'维修收入',key:'wxsr',unit:'千 元'},
    ]
    console.log('address',this.state.address)
    if(this.props.editRecord){
      console.log('items',this.props.editRecord.items)
    }
    let meterArr=this.state.meterArr

    for(let i=0;i<arr.length;i++){
      meterArr.push({
        key:arr[i].key,
        name:arr[i].name,
        unit:arr[i].unit,

      })
    }
    console.log('meterArr',meterArr)
    this.setState({meterArr})
  }
  renderColumn=(item)=>{
    let renderColumnItem=[]
    const {getFieldDecorator} = this.props.form;
    for(let i=0;i<this.state.address.length;i++){
      if(i===0){
        renderColumnItem.push(
          <FormItem
            style={{ flex: '1', marginRight: '0' }}

          >
            {getFieldDecorator(`${this.state.address[i].name}$${item.key}`, {
              initialValue: item.unit,
            })(
              <Input style={{ width: '100%', fontWeight: 'bold' }} disabled/>,
            )}
          </FormItem>
        )
      }else{
        if(this.props.editRecord){
          renderColumnItem.push(
            <FormItem
              style={{ flex: '1', marginRight: '0' }}

            >
              {getFieldDecorator(`${this.state.address[i].name}$${item.key}`, {
                initialValue: find(this.props.editRecord.items[i].items,(o)=>{
                  return o.key===item.key
                }).value,
              })(
                <Input style={{ width: '100%', fontWeight: 'bold' }}/>,
              )}
            </FormItem>
          )
        }else{
          renderColumnItem.push(
            <FormItem
              style={{ flex: '1', marginRight: '0' }}

            >
              {getFieldDecorator(`${this.state.address[i].name}$${item.key}`, {
                initialValue: '',
              })(
                <Input style={{ width: '100%', fontWeight: 'bold' }}/>,
              )}
            </FormItem>
          )
        }
      }

    }
    return renderColumnItem
  }
  render() {
    const formItemLayoutWithLabel = {
      labelCol: {
        xs: {span: 24},
        sm: {span: 8},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      }
    };
    const props = {
      onRemove: (file) => {
        this.setState(({ fileList }) => {
          const index = fileList.indexOf(file);
          const newFileList = fileList.slice();
          newFileList.splice(index, 1);
          return {
            fileList: newFileList,
          };
        });
      },
      beforeUpload: (file) => {
        this.setState(({ fileList }) => ({
          fileList: [file],
        }));
        return false;
      },
      fileList: this.state.fileList,
    };
    const {getFieldDecorator} = this.props.form;
    const meterForms = this.state.meterArr.map((item, index) => {
      return (
        <Form style={{ display: 'flex' }} className={'custom-table-form'} key={index} layout="inline">
          <div  style={{ width: '180px', lineHeight: '29px', textAlign: 'center', border: '1px solid #d9d9d9' }}>
            {item.name}
          </div>
          {this.renderColumn(item)}
        </Form>
      );
    });

    return (
      <div>
        <h1 style={{textAlign:'center'}}>产销旬报统计报告表</h1>
        <Form onSubmit={this.handleSubmit} style={{textAlign:'center'}}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               开始日期
            </span>
            )}
          >
            {getFieldDecorator('start_date', {
              initialValue: this.props.editRecord?moment(this.props.editRecord.customize.start_date):moment(),
            })(
              <DatePicker allowClear={false} />
            )}
          </FormItem>

          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               结束日期
            </span>
            )}
          >
            {getFieldDecorator('end_date', {
              initialValue: this.props.editRecord?moment(this.props.editRecord.customize.end_date):moment().add(10,'day'),
            })(
              <DatePicker allowClear={false} />
            )}
          </FormItem>
        </Form>
        <div style={{ border: '1px solid #d9d9d9' }}>
          <div style={{
            display: 'flex',
            fontWeight: 'bold',
            lineHeight: '32px',
            height: '32px',
            background: 'rgb(249,249,249)',
          }}>
            <div style={{ width: '180px', paddingLeft: '10px', border: '1px solid #d9d9d9' }}></div>
            {
              this.state.address.map((item,index)=>{
                return   <div title={item.name} key={index} style={{ flex: '1', paddingLeft: '10px', border: '1px solid #d9d9d9' }}>
                  {item.name}</div>
              })
            }
          </div>
          <div>
            {meterForms}
          </div>

        </div>
        <Form onSubmit={this.handleSubmit} style={{textAlign:'center',marginTop:'10px'}}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               部门负责人
            </span>
            )}
          >
            {getFieldDecorator('director', {
              initialValue: this.props.editRecord?this.props.editRecord.customize.director:"",
            })(
              <Input />
            )}
          </FormItem>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               制表
            </span>
            )}
          >
            {getFieldDecorator('author', {
              initialValue: this.props.editRecord?this.props.editRecord.customize.author:"",
            })(
              <Input />
            )}
          </FormItem>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={''}
          >
            {getFieldDecorator('file', {
              rules: [{required: true, message:''}],
            })(
              <Upload {...props} style={{width:'100%'}}>
                <Button type={'primary'}>上传原文件</Button>
              </Upload>
            )}

          </FormItem>
        </Form>
        <Text mark>使用方法 : </Text>
        <p>每10天1报，于每月11日、21日、31日报出，分送经理，副经理，总会计师，生产科存档。 </p>
      </div>
    );
  }
}

const AddPoliciesFormWrap = Form.create()(AddPoliciesForm);
export default connect()(AddPoliciesFormWrap);


